﻿using MQXT.Repository;
using Oliving.CommonPC.Log;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace SocketTool
{
    class Program
    {

        static void Main()
        {


            //处理未捕获的异常   
            Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
            //处理UI线程异常   
            Application.ThreadException += Application_ThreadException;
            //处理非UI线程异常   
            AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
            Application.ApplicationExit += OnApplicationExit;




            EdgeConfig.SetDbTypes(0);
            EdgeConfig.ConnectionString = ConfigurationManager.AppSettings["ConnectionString"];
            EdgeSqlSugarClient.SqlSugarClient();




            if (1 == 1)
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new FormMain());
            }
            else
            {
                ServiceBase[] ServicesToRun;
                ServicesToRun = new ServiceBase[]
                {
                new ServiceMain()
                };
                ServiceBase.Run(ServicesToRun);
            }



        }



        #region 异常捕获

        ///<summary>
        ///  这就是我们要在发生未处理异常时处理的方法，我这是写出错详细信息到文本，如出错后弹出一个漂亮的出错提示窗体，给大家做个参考
        ///  做法很多，可以是把出错详细信息记录到文本、数据库，发送出错邮件到作者信箱或出错后重新初始化等等
        ///  这就是仁者见仁智者见智，大家自己做了。
        ///</summary>
        ///<param name="sender"> </param>
        ///<param name="e"> </param>
        private static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
        {
            var ex = e.Exception;
            if (ex != null)
            {

                EdgeLog.writeLog("MainException", ex.ToString());

            }

            //MessageBox.Show("系统出现未知异常，请重启系统！");
        }

        private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            var ex = e.ExceptionObject as Exception;
            if (ex != null)
            {

                EdgeLog.writeLog("MainException", ex.ToString());

            }

            //MessageBox.Show("系统出现未知异常，请重启系统！");
        }

        private static void OnApplicationExit(object sender, EventArgs e)
        {
            //stopListen();
        }
        #endregion





    }
}
